package com.helen_dragon.healthsystem.service;/*
 *@Author: helen
 *@Date:   2021/6/18 10:43
 *@Description:
 */


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.helen_dragon.healthsystem.mapper.informationMapper;
import com.helen_dragon.healthsystem.mapper.personMapper;
import com.helen_dragon.healthsystem.model.DynamicSql;
import com.helen_dragon.healthsystem.model.information;
import com.helen_dragon.healthsystem.model.person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AdminService {


    @Autowired
    private personMapper perMapper;

    @Autowired
    private personService perService;

    @Autowired
    private informationMapper infoMapper;

    //查找出所有人员的账户信息
    public List<person> getAllPerson(){
        QueryWrapper<person> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("flag");
        List<person> list =perMapper.selectList(wrapper);
        return list;
    }

    //增加管理员用户
    public int addAdmin(person per){
        person person = perService.selectPerson(per.getPersonId());
        if(person == null) {
            int i = perMapper.insert(per);
            return i;
        }
        else
            return 0;
    }

    //编辑修改账号信息
    public void updatePerson(person person){
        perMapper.updateById(person);
    }

    //删除账号
    public void deletePerson(String personId){
        QueryWrapper<person> wrapper = new QueryWrapper<>();
        wrapper.eq("person_id", personId);
        perMapper.delete(wrapper);
    }

    //查找打卡数据
    public List<information> getDate(){
        QueryWrapper<information> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("time");
        List<information> lists = infoMapper.selectList(wrapper);
        return lists;
    }

    //通过条件查询
    public List<information> selectByCondition(DynamicSql dynamicSql){
        QueryWrapper<information> wrapper = new QueryWrapper<>();
        if(dynamicSql.getName() != "")
            wrapper.eq("name",dynamicSql.getName());
        if (dynamicSql.getPersonId() != "")
            wrapper.eq("person_id", dynamicSql.getPersonId());
        if(dynamicSql.getLatestTime() != "" && dynamicSql.getLaterTime() !="")
            wrapper.between("time", dynamicSql.getLatestTime(), dynamicSql.getLaterTime() + " 23:59:59");
        if(dynamicSql.getIdentity() != "")
            wrapper.eq("identity", dynamicSql.getIdentity());
        if(dynamicSql.getHealth() != "")
            wrapper.eq("health", dynamicSql.getHealth());
        if(dynamicSql.getKeyArea() != "")
            wrapper.eq("key_area", dynamicSql.getKeyArea());
        if(dynamicSql.getKeyPeople() != "")
            wrapper.eq("key_people", dynamicSql.getKeyPeople());
        if (dynamicSql.getTemperature() != 0.0)
            wrapper.ge("temperature", dynamicSql.getTemperature());
        wrapper.orderByDesc("time");
        List<information> lists = infoMapper.selectList(wrapper);
        return lists;
    }

    public List<person> getTodayNotSubmit(){
        List<person> allPerson = getAllPerson();
        List<person> NotSubmitLists = new ArrayList<>();
        for (person p: allPerson) {
            boolean flag = perService.is_clock_in(p.getPersonId());
            if(!flag)
                NotSubmitLists.add(p);
        }
        return NotSubmitLists;
    }

}
